package net.zhengxinyun.performance.service.departClass;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.dto.HeadColums;
import net.zhengxinyun.performance.entity.DepartClass;
import net.zhengxinyun.performance.entity.DepartClassHead;
import net.zhengxinyun.performance.entity.DepartClassItem;
import net.zhengxinyun.performance.filter.DepartClassFilter;
import net.zhengxinyun.performance.mapper.departClass.DepartClassIHeadMapper;
import net.zhengxinyun.performance.mapper.departClass.DepartClassItemMapper;
import net.zhengxinyun.performance.mapper.departClass.DepartClassMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;

@Service
@Transactional
public class DepartClassServiceImpl implements DepartClassService{
    private DepartClassMapper departClassMapper;

    @Autowired
    private DepartClassItemService itemService;

    @Autowired
    private DepartClassIHeadMapper headMapper;

    @Autowired
    private DepartClassItemMapper itemMapper;



    @Autowired
    public DepartClassServiceImpl(DepartClassMapper departClassMapper) {
        this.departClassMapper = departClassMapper;
    }


    /**
     *
     * @param departClassFilter
     * @return
     */
    @Override
    public List<DepartClassFilter> query(DepartClassFilter departClassFilter) {
        List<DepartClassFilter> lists  = departClassMapper.query(departClassFilter);
        return lists;
    }

    @Override
    public Integer queryForCount(DepartClassFilter filter) {
        return departClassMapper.queryForCount(filter);
    }

    //保存
    @Override
    public String insert(DepartClassFilter filter) {

        List<DepartClassFilter> lists = departClassMapper.query(filter) ;
        if(ObjectUtils.isNotBlank(lists))
        {
            return "已有该分类";
        }

        DepartClass dec = new DepartClass();
        BeanUtils.copyProperties(filter,dec);

        int savecode = departClassMapper.insertSelective(dec);
        if(savecode ==0)
        {
            return "save fail";
        }






        return "";
    }

    @Override
    public String update(DepartClassFilter filter) {

        DepartClass departClass = departClassMapper.selectByPrimaryKey(filter.getId());

        BeanUtils.copyProperties(filter,departClass);
        int upcode= departClassMapper.updateByPrimaryKeySelective(departClass);
        if(upcode==0)
        {
            System.out.println("保存失败");
            return "保存失败";
        }
        return null;
    }

    @Override
    public String delete(DepartClassFilter filter) {

        int delcode = departClassMapper.deleteByPrimaryKey(filter.getId());
        if(delcode==0)
        {
            System.out.println("删除失败");
            return  "删除失败";
        }
        //删除权重记录
        itemMapper.deleteByClassId(filter.getId());

        return null;
    }


    @Override
    public String saveDepartWeight(DepartClass entity) {

        departClassMapper.insertSelective(entity);
        Long classId = entity.getId();

        List<DepartClassItem> list = entity.getWeightList();
        list.stream().forEach(item -> item.setClassId(classId));

        int code = itemService.saveItems(list);
        if(code==0)
        {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "fail";

        }


        return "ok";
    }


    @Override
    public HashMap<String,Object> getDepartWeight(String datetime) {

        List<DepartClassHead> heads = headMapper.queryByDatetime(datetime);
        Map<String,String> headMap = new LinkedHashMap<>();
        int i =2;
        String cell ="cell";
        for (DepartClassHead head : heads) {

            headMap.put(head.getWeightName().trim(),cell+i);
            i++;
        }



        List<DepartClassFilter> query = departClassMapper.query(new DepartClassFilter());
        List<Map<String,Object>> dataMap = new LinkedList<>();

        for (DepartClassFilter item : query) {
            Map<String,Object> itemMap = new LinkedHashMap<>();
            itemMap.put("cell0",item.getClassname());
            itemMap.put("id",item.getId());
            List<DepartClassItem> list = itemMapper.queryDepartAllWeight(item.getId()+"", datetime);
            List<HashMap<String,Object>> datalist = new LinkedList<>();
            for (DepartClassItem classItem : list) {
                String weightName = classItem.getWeightName().trim();
                itemMap.put("datetime",classItem.getDatetime());
                if(headMap.containsKey(weightName) ){
                    itemMap.put(headMap.get(weightName),classItem.getWeight());
                }

                HashMap<String,Object> map = new HashMap<>();
                map.put("weightName",classItem.getWeightName());
                map.put("headId",classItem.getHeadId());
                map.put("weight",classItem.getWeight());
                map.put("id",classItem.getId());
                map.put("classId",classItem.getClassId());


                datalist.add(map);
            }

            itemMap.put("weightList",datalist);
            dataMap.add(itemMap);

        }

        List<HeadColums>  tableHead = new ArrayList<>();
       // Map<String, Object> map1 = dataMap.get(0);

        tableHead.add(new HeadColums("单位分类","cell0"));
        tableHead.add(new HeadColums("年份","datetime"));
        for(Map.Entry<String,String> entry:headMap.entrySet())
        {
            tableHead.add(new HeadColums(entry.getKey(),entry.getValue()));

        }

        HashMap<String,Object> gride= new HashMap<>();
        gride.put("tableHead",tableHead);
        gride.put("tableData",dataMap);
        return  gride;

    }



}
